// JavaScript Document

/*
'/*
'// ------------------------------------------------------------------------ //
'// Author             : 祁文广                               //
'//                                                                          //
'// Date        By            Description                                    //
'// ---------   ------------- -----------------------------------------------//
'// 2006/01/06  祁文广        日历		                     //
'// ------------------------------------------------------------------------ //
*/

var carldercss = '<style type="text/css"><!--#cal td{ color:4A3A24}.carlder1 {cursor: default;background-color: #7db6c7;color: #FFFFFF;}';
carldercss += '.carlder {background-color: #eeeeee;}.carlderold{cursor: default;background-color: #eeeeee;color: #999999;text-decoration: line-through;}'
carldercss += '.carlderisday{cursor: default;background-color: #7db6c7;color: #ffffff;}'
carldercss += '--></style><div id=SystemWindow style="position:absolute;z-index:1000"></div>';
document.write(carldercss);

var definput = '<input type="hidden" id="curyear"/><input type="hidden" id="curmonth"/>'
document.write(definput)

function ShowCarlder(_object,_inputobject,next_input,no_prvdate)
{
		var JsDate = new Date();
		var CurYear = JsDate.getFullYear();
		var CurMonth = JsDate.getMonth();
		
		if (isNaN(parseInt(document.getElementById('curyear').value))==false)
		{
			CurYear = parseInt(document.getElementById('curyear').value);
		}
		if (isNaN(parseInt(document.getElementById('curmonth').value))==false)
		{
			CurMonth = parseInt(document.getElementById('curmonth').value)-1;
		}
	
		delete JsDate;
		
		DrawCarlder(CurYear,CurMonth,_inputobject,next_input,no_prvdate);
		
		document.getElementById('SystemWindow').style.top = getObjPosition(document.getElementById(_object)).top+20;
		document.getElementById('SystemWindow').style.left = getObjPosition(document.getElementById(_object)).left;
		
		document.getElementById('SystemWindow').style.display = "block";
}

function DrawCarlder(CurYear,CurMonth,_inputobject,next_input,no_prvdate)
{
	no_prvdate = (no_prvdate!=null)?no_prvdate:false;
	//alert(no_prvdate)
	
	
	var html = '<table id="cal" width="150" border="0" cellpadding="1" cellspacing="1" bgcolor="#000000">';
		html += '<tr>';
		html += '<td bgcolor="#FFFFFF"><table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">';
		html += '<tr><td colspan="7" align="center" bgcolor="#7db6c7"><table width="100%" border="0" cellpadding="0" cellspacing="0">';
		html += '<tr><td width="12"><img src="images/i_previouspage.gif" onclick="javascript:CarlderMonthPrev('+CurYear+','+CurMonth+',\''+_inputobject+'\',\''+next_input+'\','+no_prvdate+')" width="7"  height="12" /></td>';
		html += '<td align="center">$ShowDate</td>';
		html += '<td width="12"><img src="images/i_nextpage.gif" onclick="javascript:CarlderMonthNext('+CurYear+','+CurMonth+',\''+_inputobject+'\',\''+next_input+'\','+no_prvdate+')" width="7" height="12" /></td>';
		html += '</tr></table></td>';
		html += '</tr>';
		html += '<tr>';;
		html += '<td width="12" align="center"><font color="Red">日</font></td>';
		html += '<td align="center">一</td>';
		html += '<td align="center">二</td>';
		html += '<td align="center">三</td>';
		html += '<td align="center">四</td>';
		html += '<td align="center">五</td>';
		html += '<td width="12" align="center"><font color="Blue">六</font></td>';
		html += '</tr><tr>';
		html += '<td colspan="7" bgcolor="#def1f8">$ShowCar</td>';
		html += '</tr><tr><td colspan="7" bgcolor="#7db6c7" align="center"><span style="cursor:hand" onclick="javascript:document.getElementById(\'SystemWindow\').style.display=\'none\'">关闭窗口</span></td></tr></table></td></tr></table>';
	


	
	var InField,EnableBack,SelectedDate;
	var StMonth = 0;
	var CurTime = new Date();
	CurTime.setFullYear(CurYear);
	CurTime.setDate(1);
	CurTime.setMonth(CurMonth);
	
	
	///每个星期的第几天
	var FirstDay = CurTime.getDay();
	
	///先是当前年月
	//document.getElementById('MonthShow').innerHTML = '<a href="javascript:MonthPrev(\''+CurYear+'\',\''+CurMonth+'\')">上一月</a>　'+CurYear + '年' + (CurMonth + 1) + '月'+'  <a href="javascript:MonthNext(\''+CurYear+'\',\''+CurMonth+'\')">下一月</a>　';
	html = html.replace('$ShowDate',CurYear + '年' + (CurMonth + 1) + '月')
	///判断是否为闰年，闰年2月为28天
	if(CurYear.toString().substring(2) != '00' && CurYear % 4 != 0)
	{
		StMonth = 28;
	}
	else
	{
		StMonth = 29;
	}
	
	///创建12个月每月天数
	var YearMonth = new Array(31,StMonth,31,30,31,30,31,31,30,31,30,31);
	///本月天数
	var MonthDayCount = YearMonth[CurMonth];
	///创建显示日历的表格
	var mytb = document.createElement('table');
	mytb.cellPadding = 1;
	mytb.cellSpacing = 1;
	mytb.border = 0;
	mytb.bgcolor="#ffffff"
	mytb.width = '100%'
		
	///表格行，列
	var tRow = new Object(),tCell = new Object();
	var Cfmd = new Date();
	var ToDay = new Date();
	///行数，日数
	var i,CellCount = 0,DayCount = 1,Fed = false;
	
	/*var tmptable = '<table cellPadding="1" cellSpacing="1" border="0" bgcolor="#ffffff" width="100%">';
	
	for (var week = 0; week <=5; week++)
	{
		tmptable += '<tr>';
		for (i = 0; i<=6 ; i++)
		{
			tmptable += '<td align="center"';
			if (CellCount == FirstDay && week == 0 && Fed == false)
			{
				
				
				if(CurYear < Cfmd.getFullYear() || CurYear == Cfmd.getFullYear() && CurMonth == Cfmd.getMonth() && DayCount < Cfmd.getDate() ||  CurYear == Cfmd.getFullYear() && CurMonth < Cfmd.getMonth())
				{
					tmptable += ' class="carlderold">1';

				}
				else
				{
					//tmptable += 'OnClick = "javascript:SelectTime(\''+_inputobject+'\','+ CurYear + '-' + parseInt(CurMonth+1) + '-1,\''+next_input+'\'")';
					//tmp table += ' class= "carlder1" onmousemove = "this.className=\'carlder1\'" onmouseout="this.className=\'carlder\'">1'
					/*tCell.innerText = '1';
					tCell.	
					tCell.onmousemove = "this.className= 'carlder1'";
					tCell.onmouseout="this.className= 'carlder'";
					if(CurYear == Cfmd.getFullYear() && CurMonth == Cfmd.getMonth() && DayCount == Cfmd.getDate())
					{
						tCell.className = 'carlderisday';	
						tCell.onmouseout="this.className= 'carlderisday'";
					}
				}	
			}
			else
			{
				tmptable += '>'+DayCount				
			}
			
			DayCount++;
			CellCount++;
		}
		tmptable += '</tr>'
	}*/
	
	for(var week = 0 ; week <= 5 ; week++)
	{
		///建立新行
		tRow = mytb.insertRow(week);
		for(i = 0 ; i <= 6 ; i++)
		{
			///新列
			tCell = tRow.insertCell(i);
			tCell.Id = "c"+DayCount;
			tCell.align = 'center';
			tCell.className = "carlder";

			//if(CurYear < Cfmd.getFullYear() || CurYear == Cfmd.getFullYear() && CurMonth == Cfmd.getMonth() && DayCount < Cfmd.getDate() ||  CurYear == Cfmd.getFullYear() && CurMonth < Cfmd.getMonth())
			//{
			//	tCell.className = "carlderold";
			//}
		
			if (CellCount == FirstDay && week == 0 && Fed == false)
			{
				if((CurYear < Cfmd.getFullYear() || CurYear == Cfmd.getFullYear() && CurMonth == Cfmd.getMonth() && DayCount < Cfmd.getDate() ||  CurYear == Cfmd.getFullYear() && CurMonth < Cfmd.getMonth()) && no_prvdate == false)
				{
					tCell.className = "carlderold";
					tCell.innerText = '1';
				}
				else
				{
					
					tCell.innerText = '1';
					tCell.OnClick = "javascript:SelectTime('"+_inputobject+"','"+ CurYear +'-'+ parseInt(CurMonth+1) +"-1','"+next_input+"')";	
					tCell.onmousemove = "this.className= 'carlder1'";
					tCell.onmouseout="this.className= 'carlder'";
					if(CurYear == Cfmd.getFullYear() && CurMonth == Cfmd.getMonth() && DayCount == Cfmd.getDate())
					{
						tCell.className = 'carlderisday';	
						tCell.onmouseout="this.className= 'carlderisday'";
					}
				}
				DayCount++;
				Fed = true;
				CellCount++;
			}
			else if(CellCount != FirstDay && week == 0 && Fed == false)
			{
				CellCount++;
				continue;
			}
			else
			{
				if((CurYear < Cfmd.getFullYear() || CurYear == Cfmd.getFullYear()  && CurMonth == Cfmd.getMonth() && DayCount < Cfmd.getDate() ||  CurYear == Cfmd.getFullYear() && CurMonth < Cfmd.getMonth()) && no_prvdate == false)
				{
					tCell.className = "carlderold";
					tCell.innerText = DayCount;
				}
				else
				{
					tCell.innerText = DayCount ;
					tCell.OnClick = "javascript:SelectTime('"+_inputobject+"','"+ CurYear +'-'+  parseInt(CurMonth+1) +"-"+DayCount+"','"+next_input+"')";
					tCell.onmousemove = "this.className= 'carlder1'";
					tCell.onmouseout="this.className= 'carlder'";
					if(CurYear == Cfmd.getFullYear() && CurMonth == Cfmd.getMonth() && DayCount == Cfmd.getDate())
					{
						tCell.className = 'carlderisday';	
						tCell.onmouseout="this.className= 'carlderisday'";
					}
				}
				DayCount++;
				CellCount++;
			}
			if((DayCount - 1) == MonthDayCount)
			{
				break;
			}
		}
		if((DayCount - 1) == MonthDayCount)
		{
			break;
		}
	}
	
	//document.write(html.replace('$ShowCar',mytb.outerHTML))
	//var ifram = 
	document.getElementById('SystemWindow').innerHTML = html.replace('$ShowCar',mytb.outerHTML);//mytb.outerHTML  tmptable
	
}

function SelectTime(inputname,str,next_input)
{
	document.getElementById(inputname).value=str
	str = str.split('-');
	if (str[0]!=null)
	{
		document.getElementById('curyear').value=str[0];
	}
	if (str[1]!=null)
	{
		document.getElementById('curmonth').value=str[1];
	}
	document.getElementById('SystemWindow').style.display = 'none';
	if (next_input!="")
	{
		document.getElementById(next_input).focus();
	}
	
	
	///此处使用选择日期后的外部处理命令，因为想不出其他的办法，所以就用错误处理，看叶面中是否存在这个处理过程-_-,白痴的做法
	try
	{
		if (inputname == document.getElementById('ActionHiddenCo').value)
		{
			SelectTimeAction();
		}
	}
	catch(e)
	{}
}

function CarlderMonthPrev(CurYear,CurMonth,_inputobject,next_input,no_prvdate)
{
		if(CurMonth == 0){
			CurMonth = 12;
			CurYear--;
		}
		CurMonth--;
		DrawCarlder(CurYear,CurMonth,_inputobject,next_input,no_prvdate);

}

function CarlderMonthNext(CurYear,CurMonth,_inputobject,next_input,no_prvdate)
{
		if(CurMonth == 11){
			CurMonth = -1;
			CurYear++;
		}
		CurMonth++;
		DrawCarlder(CurYear,CurMonth,_inputobject,next_input,no_prvdate);
}
/*
获取物件坐标
*/

function getObjPosition(_object)
{
	var _top = _object.offsetTop;
	var _left = _object.offsetLeft;
	while(_object = _object.offsetParent)
	{
		_top += _object.offsetTop;
		_left += _object.offsetLeft;
	}
	return{ top: _top, left: _left };
}